#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/07/22
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import json, session
from project.define import AxiosMessage, PermissionsModule
from project.modules import Custom, User
from .network import InterFaces



class Messager(AxiosMessage):
    """
    消息管理器
    """
    def __init__(self) -> None:
        super().__init__()
        
    def __generate_page_data(self, *args):
        """
        生成页面数据
        """
        return {
            "ColumnName":args[0], 
            "ColumnNameJSON": json.dumps(args[0],ensure_ascii=False), 
            "TableDatas": args[1],
        }

    def ____form_page_data(self, *args):
        """
        表单页面数据
        """
        return {
            "ColumnName":args[0], 
            "ColumnNameJSON": json.dumps(args[0],ensure_ascii=False), 
            "SettingFormData": json.dumps(args[1],ensure_ascii=False)
        }


    def setting_page_data(self):
        """
        闸机页面
        """
        setting = Custom.query.first()
        column_name = Custom.columns_name()
        return self.____form_page_data(*[column_name, setting.to_dict()])

    def network_page_data(self):
        """
        网络设置
        """
        column_name = {
            "ifname":"接口名稱","mac":"MAC地址","state":"連接狀態","type":"接口類型","method":"網絡模式","addresses":"IP地址",
            "gateway":"網關地址","dns":"DNS地址","ssid":"WiFi信號","password":"WiFi密碼"}
        ret = [i.conn_info() for i in InterFaces]
        return {"ColumnName":json.dumps(column_name,ensure_ascii=False),"InterFacesInfo":json.dumps(ret,ensure_ascii=False)}

    def user_page_data(self):
        """
        用户管理
        """
        column_name = User.columns_name()
        users = User.query.filter_by(Deleted=False).all()
        return self.__generate_page_data(column_name,users)
